<template>
  <el-dialog
      :title="$t('m.Please_Bind_Account')"
      :visible.sync="visible"
      width="400px"
      @close="handleClose"
  >
    <el-form
        :model="formData"
        :rules="rules"
        ref="bindForm"
        label-width="100px"
    >
      <el-form-item :label="$t('m.Remote_JUDGE')" prop="ojName">
        <span>{{ ojName }}</span>
      </el-form-item>

      <el-form-item :label="$t('m.Username')" prop="username">
        <el-input v-model="formData.username" :placeholder="$t('m.Please_Enter_Username')"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('m.Password')" prop="password">
        <el-input
            v-model="formData.password"
            type="password"
            :placeholder="$t('m.Please_Enter_Password')"
            show-password
        ></el-input>
      </el-form-item>

      <el-form-item v-if="captchaVisible" :label="$t('m.Captcha')" prop="captcha">
        <el-input v-model="formData.captcha" :placeholder="$t('m.Please_Enter_Captcha')">
          <template slot="append">
            <img :src="captchaUrl" @click="refreshCaptcha" alt="captcha" class="captcha-img">
          </template>
        </el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('m.Cancel') }}</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">{{ $t('m.OK') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import utils from "@/common/utils";
import api from "@/common/api";
import mMessage from "@/common/message";

export default {
  name: 'BindRemoteJudgeAccount',
  props: {},
  data() {
    return {
      visible: false,
      oj: '',
      ojName: '',
      captchaVisible: false,
      captchaUrl: '',
      submitting: false,
      formData: {
        username: '',
        password: '',
        captcha: ''
      },
      rules: {
        username: [
          {required: true, message: this.$t('m.Username_Required'), trigger: 'blur'},
          {min: 3, max: 20, message: this.$t('m.Username_Length_Check'), trigger: 'blur'}
        ],
        password: [
          {required: true, message: this.$t('m.Password_Required'), trigger: 'blur'},
          {min: 6, max: 20, message: this.$t('m.Password_Length_Check'), trigger: 'blur'}
        ],
        captcha: [
          {required: true, message: this.$t('m.Captcha_Required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    open(oj) {
      this.oj = oj
      this.captchaVisible = 'LUOGU' === oj
      this.ojName = utils.getRemoteOjName(oj)
      this.refreshCaptcha()
      this.visible = true
    },
    handleClose() {
      this.$refs.bindForm.resetFields()
      this.visible = false
    },
    refreshCaptcha() {
      this.captchaUrl = `/api//remote-judge-login-captcha?t=${new Date().getTime()}&oj=${this.oj}`
    },
    handleSubmit() {
      this.$refs.bindForm.validate((valid) => {
        if (!valid) {
          return
        }
        const data = Object.assign({}, this.formData);
        data.oj = this.oj;

        this.submitting = true;
        api.verifyRemoteJudgeAccount(data).then(
            (res) => {
              this.submitting = false
              this.handleClose()
              mMessage.success(this.$i18n.t('m.Bind_Successfully'))
            },
            (_) => {
              this.submitting = false
              this.refreshCaptcha()
            }
        );  // end api.verifyRemoteJudgeAccount
      }) // end validate
    }
  }
}
</script>

<style scoped>
.el-dialog__body {
  padding: 20px 20px 0;
}

.captcha-img {
  display: block;
  height: 40px;
  margin: -1px -20px;
  cursor: pointer;
}

.el-input-group__append {
  padding: 0;
  line-height: 0;
}
</style>
